function group_id=h5groupput ( h5file, groupname)
% H5GROUPPUT  Place group in HDF5 file.
%
%   GROUP_ID=H5GROUPPUT(H5FILE,GROUPNAME) creates the group named by
%   GROUPNAME.  H5FILE can be the name of an HDF5 file or a file ID.
%   GROUP_ID is a group identifier.  If GROUP_ID is not requested, it will 
%   be closed before the function finishes.
%
%   If GROUPNAME's parent group does not exist, it will be created.
%
%   Example:  
%       h5filecreate('myfile.h5');
%       h5groupput('myfile.h5','/a/b/c/d');
%       h5groupput('myfile.h5','/The/HDF/Group');
%       h5dump('myfile.h5');
%
%   See also:  h5filecreate.

%   Copyright 2010 The MathWorks, Inc.
%   Full credit to Philip Top (LLNL).


if ~ischar(groupname)
    error('MATLAB:H5GROUPPUT:badDatatype', ...
        'GROUPNAME input argument must have class char.' );
end

if ~ischar(h5file)
    if ~isequal(class(h5file),'H5ML.id')
        error('MATLAB:H5GROUPPUT:badDatatype', ...
            'H5FILE input argument must have either class char or datatype H5ML.id.' );
        
    end
end

flags = 'H5F_ACC_RDWR';
plist_id = 'H5P_DEFAULT';

if ischar(h5file)
    file_id = H5F.open ( h5file, flags, plist_id );
else
    file_id=h5file;
end


% 2008b and below cannot create intermediate groups.
switch(version('-release'))
case { '2006b','2007a', '2007b', '2008a', '2008b'}
    group_id=H5G.create(file_id,groupname,0);
otherwise
    lcpl = H5P.create('H5P_LINK_CREATE');
	H5P.set_create_intermediate_group(lcpl,1);
    group_id=H5G.create(file_id,groupname,lcpl,'H5P_DEFAULT','H5P_DEFAULT');
end



%if no output arguments we need to close the group
if (nargout==0)
    H5G.close(group_id);
end

if ischar(h5file)
    H5F.close(file_id);
end


end
